import 'package:get/get.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/preventive_rectification_get_entity.dart';
import 'package:safetymonitor/modules/home/snap_shot_danger/no_completed/controller.dart';
///整改中
class UnderRectificationStateController extends GetxController {
  PreventiveRectificationGetEntity preventiveRectificationGetEntity=PreventiveRectificationGetEntity();
  /// 当前选中的标题索引，0：基本信息 1:隐患整改
  RxInt currentIndex = 0.obs;
  ///	隐患上报视频集合[数组]
  List<String>? dangerVideo = [];
  List<String>? dangerImages = [];

  ///附件 字符串逗号隔开
  String fileUrl = '';
  ///整改进度
  String rectificationProgress = '';
  ///完成情况
  String rectificationActions = '';
  ///整改措施
  String rectificationDesc = '';
  @override
  void onInit() {
    super.onInit();
    initData();
  }
  /// 初始化数据
  Future<void> initData() async {
    //获取组织数据
    final res = await HttpUtil().generateGetRequest<PreventiveRectificationGetEntity>(
        Interface.preventiveRectificationGet,
        queryParameters: {
          'id': Get.arguments
        }).execute(isShowLoading: true);
    res.onSuccess((result) {
      if (result.data != null) {
        preventiveRectificationGetEntity= result.data ??PreventiveRectificationGetEntity();
      }
      update();
    });
  }


  /// 提交
  void submit() async {
    //判断验收 和描述
    if(fileUrl.trim().isEmpty){
      AppToast.showWarning('请选择选择附件');
      return;
    }
    if(rectificationProgress.trim().isEmpty){
      AppToast.showWarning('请输入完成情况');
      return;
    }
    if(int.parse(rectificationProgress)>100){
      AppToast.showWarning('请输入完成情况，小于100的整数');
      return;
    }
    if(int.parse(rectificationProgress)<1){
      AppToast.showWarning('请输入完成情况，大于1的整数');
      return;
    }
    if(rectificationDesc.trim().isEmpty){
      AppToast.showWarning('请输入整改进度');
      return;
    }
    if(rectificationActions.trim().isEmpty){
      AppToast.showWarning('请输入整改措施');
      return;
    }

    final res = await HttpUtil()
        .generatePostRequest(
      Interface.rectificationRecordCreate,
      data: {
        'preventiveId':  Get.arguments,
        'rectificationProgress': rectificationProgress,
        'rectificationActions':rectificationActions,
        'rectificationDesc':rectificationDesc,
        'rectificationFile': fileUrl
      },
    )
        .execute(isShowLoading: true);
    res.onSuccess((result) {
      Get.back(result: true);
      AppToast.showWarning('提交成功');
      Get.find<NoCompletedController>().globalKey.currentState?.onRefresh();
    });
  }
  /// title选择切换
  void selectTitle(int index) {
    currentIndex.value = index;
  }
}
